﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CommunityServer.WebServices.MediaGalleries;
using System.Web;

namespace nb.development.csmediamanager
{
    public class TagWorker
    {
        public TagWorker(string path, Site site)
        {
            this.site = site;
            this.path = path;
        }

        private Site site;
        private string path;
        public event TagWorkerCompleted Completed;

        public void Run()
        {
            try
            {
                MediaGalleriesService mgs = site.Service;
                int id = int.Parse(path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)[2]);
                List<string> tags = new List<string>();
                foreach (MediaGalleryPost post in mgs.GetMediaGalleryPosts(id, 1, 200))
                    foreach (string s in post.Keywords)
                        if (!tags.Contains(HttpUtility.HtmlDecode(s))) tags.Add(HttpUtility.HtmlDecode(s));
                if (Completed != null)
                    Completed(tags, path);
            }
            catch (Exception ex) { MessageBox.Show("Error:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }

    public delegate void TagWorkerCompleted(List<string> tags, string path);
}
